<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="btnCancel">
    <!-- 分配角色 -->
    <el-checkbox-group v-model="roleIdsList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{
          item.name
        }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="end">
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 总角色数组
      roleList: [],
      //   多选框选中的角色数组
      roleIdsList: []
    }
  },
  async created() {
    const { rows } = await getRoleList()
    this.roleList = rows
  },
  methods: {
    //   回显数据
    async getUserDetailById(id) {
      const res = await getUserDetailById(id)
      this.roleIdsList = res.roleIds
      console.log(res)
    },
    async btnOk() {
      await assignRoles({
        id: this.userId,
        roleIds: this.roleIdsList
      })
      this.$message.success('操作成功')
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
